/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ivanbiz.kelas;

import com.ivanbiz.account.Account;
import com.ivanbiz.account.AccountDAO;
import com.ivanbiz.siswa.Siswa;
import com.ivanbiz.siswa.SiswaDAO;
import java.util.List;

/**
 *
 * @author User
 */
public class KelasServiceImpl implements KelasService {

    private KelasDAO kelasDAO;
    private AccountDAO accountDAO;
    private SiswaDAO siswaDAO;

    public void setSiswaDAO(SiswaDAO siswaDAO) {
        this.siswaDAO = siswaDAO;
    }

    public void setAccountDAO(AccountDAO accountDAO) {
        this.accountDAO = accountDAO;
    }

    public void setKelasDAO(KelasDAO kelasDAO) {
        this.kelasDAO = kelasDAO;
    }

    public void saveKelas(Kelas kelas) throws Exception {
        this.kelasDAO.save(kelas);
    }

    public void deleteKelas(Kelas kelas) throws Exception {
        this.kelasDAO.delete(kelas);
    }

    public void updateKelas(Kelas kelas) throws Exception {
        this.kelasDAO.update(kelas);
    }

    public List loadAll() throws Exception {
        return this.kelasDAO.getAll(Kelas.class);
    }

    public Kelas loadById(long id) throws Exception {
        return (Kelas) this.kelasDAO.load(Kelas.class, id);
    }

    public Kelas loadByUserId(long id) throws Exception {
        Kelas kelas = null;
        Account account = (Account) this.accountDAO.loadByUserId(id);
        if (account == null) {
            Siswa siswa = (Siswa) this.siswaDAO.loadByUserId(id);
            kelas = (Kelas) this.kelasDAO.loadByAccountId(siswa.getId());
        } else {
            kelas = (Kelas) this.kelasDAO.loadByAccountId(account.getId());
        }
        return kelas;
    }
}
